package com.rrqs.activity.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.rrqs.activity.OrderDetailNewActivity;
import com.rrqs.activity.common.Common;
import com.rrqs.activity.common.Const;
import com.rrqs.activity.data.OrderNewData;
import com.rrqs.views.CountDownTimer;
import com.rrqs.views.OnChanged;
import com.zadtek.xmqishi.R;

import java.util.List;

public class OrderNewAdapter extends CommonAdapter<OrderNewData> {
	private OnChanged listener;
	private SparseArray<CountDownTimer> timers;

	public OrderNewAdapter(Context context, List<OrderNewData> datas,
			int layoutId) {
		super(context, datas, layoutId);
		timers = new SparseArray<>();
		// TODO Auto-generated constructor stub
	}

	@Override
	public void convert(final ViewHolders holder, final OrderNewData t,int position) {
		View view =holder.getmConvertView();

		TextView newtaskmomey = holder.getView(R.id.newtask_money);
		TextView newtasktime = holder.getView(R.id.newtask_time);
		TextView newtask_goods_getaddress = holder
				.getView(R.id.newtask_goods_getaddress);
		TextView getgpsaddr = holder.getView(R.id.getgpsaddr);
		newtaskmomey.setText("¥" + t.getReturnpay());
		if (!t.getAdd_time().equals("") && t.getAdd_time() != null && !t.getAdd_time().equals("null")) {
			newtasktime.setText(Common.getStrTime(t.getAdd_time()));
		}
		newtask_goods_getaddress.setText(t.getSendaddress());
		try {
			String address_road =t.getAddress_road();
			if (address_road ==null || address_road.equals("") || address_road.equals("null")) {
				
			String addrss = t.getGps_addrs();
			if (addrss != null && !addrss.equals("")) {
				getgpsaddr.setText(addrss); // 根据经纬度反查地址
				getgpsaddr.setVisibility(View.VISIBLE);
			} else {
				getgpsaddr.setVisibility(View.GONE);
			}
			}else{
				getgpsaddr.setText(address_road); // 根据经纬度反查地址
				getgpsaddr.setVisibility(View.VISIBLE);
			}
		} catch (Exception e) {
		}
		
		ListView ItemList = holder.getView(R.id.newtask_itemlist);
		Button submit = holder.getView(R.id.submit);
		Button zhuan = holder.getView(R.id.zhuan);
		LinearLayout show_layout1 =holder.getView(R.id.show_layout1);
//		if (t.getTime_limit().equals("0")) {
			submit.setText("确认");
//		submit.setText("抢单(系统)");
//		}else{
//			submit.setText("抢单(还剩"+t.getTime_limit()+"秒)");
//		}

		OrderNewItemAdapter itemadapter = new OrderNewItemAdapter(mContext,
				t.getOrderItemList(), R.layout.newtasklist_itemlist_item);
		ItemList.setAdapter(itemadapter);

		submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				SharedPreferences sp = mContext.getSharedPreferences(
						Const.PREF_NAME, Context.MODE_PRIVATE);
				String Uid = sp.getString("uid", "");
				if (null != Uid && !Uid.equals("")) {
					listener.getChoiceData(t.getMainid(), Uid,holder.getPositon());
				}
			}
		});

		zhuan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO 骑手转单
				SharedPreferences sp = mContext.getSharedPreferences(Const.PREF_NAME, Context.MODE_PRIVATE);
				String Uid = sp.getString("uid", "");
				if (null != Uid && !Uid.equals("")) {
					listener.zhuandan(t.getMainid(), Uid,holder.getPositon());
				}
			}
		});


		holder.getView(R.id.ll_main).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, OrderDetailNewActivity.class);
				intent.putExtra("mainid", t.getMainid());
				intent.putExtra("type", t.getDingdantype());
				mContext.startActivity(intent);
			}
		});
		//时间计时器相关
		TextView orderStartTime = holder.getView(R.id.orderStartTime);
		TextView orderEndTime = holder.getView(R.id.orderEndTime);
		TextView order_end_time_stamp = holder.getView(R.id.order_end_time_stamp);
		ProgressBar progress = holder.getView(R.id.progress);
		orderStartTime.setText(t.getOrder_start_time());
		orderEndTime.setText(t.getOrder_end_time());
		progress.setProgress((int) (0.8 * 100));


		int order_end_time_stamp1 = t.getOrder_end_time_stamp();
		//获取当前时间戳
//		long timeStamp = System.currentTimeMillis() / 1000;
		long timeStamp = 0;

		CountDownTimer countDownTimer = timers.get(order_end_time_stamp.hashCode());
		if (countDownTimer != null) {
			countDownTimer.cancel();
		}

		long s1 = order_end_time_stamp1 - timeStamp;
		long s2 = timeStamp - order_end_time_stamp1;
		if (s1 > 0) {
			countdown(s1, order_end_time_stamp, false, s2);
		} else {
//			if (s1 + 36000 > 0) {
//				countdown((s1 + 36000), order_end_time_stamp, true, s2);
//			} else {
				order_end_time_stamp.setText("已超时");
//			}
		}

		if (!t.getAdd_time().equals("") || t.getAdd_time() != null) {
			newtasktime.setText(Common.getStrTime(t.getAdd_time()));
		}
		//新增字段相关
		TextView tv_code = holder.getView(R.id.number);
		TextView tv_type = holder.getView(R.id.type);
		TextView tv_phone = holder.getView(R.id.phone);
		tv_code.setText("商家流水号："+t.getLiushui());
		if(t.getDingdantype()==1){
			tv_type.setText("订单类型：系统派单");
		}else{
			tv_type.setText("订单类型：人工派单");

		}
		if(t.getZhuandan().equals("1")){
			zhuan.setVisibility(View.VISIBLE);
		}else{
			zhuan.setVisibility(View.GONE);
		}
		tv_type.setVisibility(View.INVISIBLE);
		tv_phone.setText("顾客手机号："+t.getUser_phone());


//		try {
//			String address_road = t.getAddress_road();
//			if (address_road == null || address_road.equals("") || address_road.equals("null")) {
//				String addrss = t.getGps_addrs();
//				if (addrss != null && !addrss.equals("")) {
//					getgpsaddr.setText(addrss); // 根据经纬度反查地址
//					getgpsaddr.setVisibility(View.VISIBLE);
//				} else {
//					getgpsaddr.setVisibility(View.GONE);
//				}
//			} else {
//				getgpsaddr.setText(address_road); // 根据经纬度反查地址
//				getgpsaddr.setVisibility(View.VISIBLE);
//			}
//		} catch (Exception e) {
//		}
	}

	public void setOnChanged(OnChanged listener) {
		this.listener = listener;
	}
	private void countdown(final long s, final TextView tv, final boolean isTimeOut, final long s2) {
		/** 倒计时s秒，一次1秒 */
		CountDownTimer timer = new CountDownTimer(s * 1000, 1000) {
			@Override
			public void onTick(long millisUntilFinished) {
				// TODO Auto-generated method stub
				long ss;
				if (isTimeOut) {
					ss = s2 + s - millisUntilFinished / 1000;
				} else {
					ss = millisUntilFinished / 1000;
				}
				if (ss < 60) {
					if (isTimeOut) {
						tv.setText("超时" + ss + "秒");
					} else {
						tv.setText("剩余" + ss + "秒");
					}
				} else if (millisUntilFinished < 3600) {
					if (isTimeOut) {
						tv.setText("超时" + ss / 60 + "分" + ss % 60 + "秒");
					} else {
						tv.setText("剩余" + ss / 60 + "分" + ss % 60 + "秒");
					}
				} else {
					if (isTimeOut) {
						tv.setText("超时" + ss / 3600 + "时" + ss % 3600 / 60 + "分" + ss % 60 + "秒");
					} else {
						tv.setText("剩余" + ss / 3600 + "时" + ss % 3600 / 60 + "分" + ss % 60 + "秒");
					}
				}
			}

			@Override
			public void onFinish() {
				if (isTimeOut) {
					tv.setText("已超时");
				} else {
					countdown(36000, tv, true, 0);
				}
			}
		}.start();

		timers.put(tv.hashCode(), timer);
	}

	@Override
	protected void finalize() throws Throwable {
		super.finalize();
		timers.clear();
	}
}
